#include <mutex>
#include <shared_mutex>

#include "element.h"

namespace Elements
{

Element::Element(const Element& other)
    : m_lock()
    , m_id(other.m_id)
{
}

std::string Element::toJsonString(bool isPretty) const
{
    return toJson().dump(isPretty ? 4 : -1);
}

unsigned Element::id() const
{
    std::shared_lock locker(m_lock);
    return m_id;
}

void Element::setId(unsigned value)
{
    std::unique_lock locker(m_lock);
    m_id = value;
}

std::ostream& operator<<(std::ostream& out, const Element* const element)
{
    if (nullptr == element)
        return out;

    out << "---" << std::endl
        << element->toJsonString(true);
    return out;
}

} // namespace Elements
